
/** A move of a box on a board.
 * from the square start to the square end
 * a move can only be one square long so it can be
 * represented by only the square start and a direction
 * 
 * @author romain
 *
 */
public class Move {
	
	private Position start;
	private Position end;
	private byte direction;
	public boolean valid=true;
	
	static byte MOVE_UP =  1 << 0;
	static byte MOVE_DOWN =  1 << 1;
	static byte MOVE_LEFT =  1 << 2;
	static byte MOVE_RIGHT =  1 << 3;
	
	
	public Move(Position start, byte d){
		this.start = start;
		this.direction = d;
		if ((d&MOVE_UP) !=0) this.end = new Position(start.getI()-1,start.getJ());
		else if ((d&MOVE_DOWN) !=0) this.end = new Position(start.getI()+1,start.getJ());
		else if ((d&MOVE_LEFT) !=0) this.end = new Position(start.getI(),start.getJ()-1);
		else if ((d&MOVE_RIGHT) !=0) this.end = new Position(start.getI(),start.getJ()+1);
		else System.out.println("Bad move !!!");
	}
	
	public Move(Position start, Position end){
		this.start = start;
		this.end = end;
		if (start.getI()-1 == end.getI()) this.direction = MOVE_UP;
		else if (start.getI()+1 == end.getI()) this.direction = MOVE_DOWN;
		else if (start.getJ()-1 == end.getJ()) this.direction = MOVE_LEFT;
		else if (start.getJ()+1 == end.getJ()) this.direction = MOVE_RIGHT;
		else System.out.println("Bad move !!!");
	}
	
	public String toString(){
		String re;
		re = ("-->Move ");
		re += ("  start "+this.start);
		re += (" |  end "+this.end);
		if ((this.direction&MOVE_UP) !=0) re+= ("  direction MOVE_UP");
		else if ((this.direction&MOVE_DOWN) !=0) re+=("  direction MOVE_DOWN");
		else if ((this.direction&MOVE_LEFT) !=0)  re+=("  direction MOVE_LEFT");
		else if ((this.direction&MOVE_RIGHT) !=0)  re+=("  direction MOVE_RIGHT");
		return re;
	}

	public Position getStart() {
		return start;
	}

	public Position getEnd() {
		return end;
	}

	public byte getDirection() {
		return direction;
	}
	public void setValid(boolean valid)
	{
		this.valid=valid;
	}
	
	public boolean isEqual(Move m){
		if (this.start.getI() == m.start.getI()
				&& this.end.getI() == m.start.getI()
				&& this.start.getJ() == m.start.getJ()
				&& this.end.getJ() == m.end.getJ())
			return true;
		else return false;
	}

}
